package com.wenwan.manage.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwan.manage.mapper.GiveGetCommentMapper;
import com.wenwan.manage.pojo.GiveGetComment;
import com.wenwan.manage.pojo.LimitPage;
import com.wenwan.manage.service.GiveGetCommentService;
import com.wenwan.manage.setting.RegExpSetting;
import com.wenwan.manage.setting.StatusSetting;
import com.wenwan.manage.util.Util;

@Service
public class GiveGetCommentServiceImpl implements GiveGetCommentService {

	@Autowired
	private GiveGetCommentMapper giveGetCommentMapper;

	@Override
	public Map<String, Object> create(String content, String giveGetId, String userId) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (content != null && content.length() < 20) {
			dto.put("data", false);
			dto.put("msg", "content" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_FIFTEEN_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("content" +
			// StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_FIFTEEN_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (giveGetId != null && !giveGetId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "giveGetId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("giveGetId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (userId != null && !userId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", Util.UUID());
		map.put("createTime", Util.getTime());
		map.put("updateTime", Util.getTime());
		map.put("content", content);
		map.put("giveGetId", giveGetId);
		map.put("userId", userId);

		Boolean data = giveGetCommentMapper.create(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> deleteById(String id) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);

		Boolean data = giveGetCommentMapper.deleteById(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> update(String id, String content, String giveGetId, String userId) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (content != null && content.length() < 20) {
			dto.put("data", false);
			dto.put("msg", "content" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_FIFTEEN_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("content" +
			// StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_FIFTEEN_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (giveGetId != null && !giveGetId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "giveGetId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("giveGetId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (userId != null && !userId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("content", content);
		map.put("giveGetId", giveGetId);
		map.put("userId", userId);
		map.put("updateTime", Util.getTime());

		return dto;
	}

	@Override
	public Map<String, Object> selectAll(Integer currentPage, Integer pageSize, String giveGetId) {
		// TODO Auto-generated method stub
		// DataToObject<List<GiveGetComment>> dto = new
		// DataToObject<List<GiveGetComment>>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (currentPage != null && currentPage < 1) {
			dto.put("data", false);
			dto.put("msg", "currentPage" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("currentPage" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (pageSize != null && pageSize < 1) {
			dto.put("data", false);
			dto.put("msg", "pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (giveGetId != null && !giveGetId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "giveGetId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("giveGetId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("currentPage", (currentPage - 1) * pageSize);
		map.put("pageSize", pageSize);
		map.put("giveGetId", giveGetId);

		List<GiveGetComment> data = giveGetCommentMapper.selectAll(map);

		LimitPage limitPage = new LimitPage();
		limitPage.setCurrentPage(currentPage);
		limitPage.setPageSize(pageSize);
		Integer maxSize = giveGetCommentMapper.selectMaxSize(map);
		limitPage.setMaxSize(maxSize);

		// dto.setLimitPage(limitPage);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("limitPage", limitPage);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> selectOneById(String id) {
		// TODO Auto-generated method stub
		// DataToObject<GiveGetComment> dto = new DataToObject<GiveGetComment>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);

		GiveGetComment data = giveGetCommentMapper.selectOneById(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

}
